import { observable, action, toJS } from 'mobx';
import { Tools, Toast } from 'weaver-mobile';
import { Base64 } from 'js-base64';

export default class EntranceBasicStore {
  @observable accountLogin = false;
  @observable loading = true;
  @observable hasError = false;
  @observable errorMsg = '';
  @observable account = '';
  @observable password = '';
  @observable rememberAccount = true;
  @observable rememberPassword = true;

  constructor(props) {
    if (Tools.ls.getStr('emMobileAccount')) {
      this.account = Base64.decode(Tools.ls.getStr('emMobileAccount'));

      if (Tools.ls.getStr('emMobilePassword')) {
        this.password = Base64.decode(Tools.ls.getStr('emMobilePassword'));
      }
    }
  }

  @action
  setState = (params = {}) => {
    Object.keys(params).forEach((key) => {
      this[key] = params[key];
    });
  }

  @action
  loginSuccess = () => {
    Toast.hide();
    Toast.info('登录成功', 2, () => {
      if (window.e9ssoMobileConfig && window.e9ssoMobileConfig.inUse && window.e9ssoMobileConfig.homePageUrl) {
        window.location.href = window.e9ssoMobileConfig.homePageUrl;
      } else {
        window.location.href = '/spa/em/mobile.html'
      }
    });
  }

  @action
  loginFail = (error = '') => {
    Toast.hide();
    Toast.info('登录失败');
  }

  @action
  doLogin = () => {
    if (this.rememberAccount) {
      Tools.ls.set('emMobileAccount', Base64.encode(toJS(this.account)))
    } else {
      Tools.ls.set('emMobileAccount', '')
    }
    if (this.rememberPassword) {
      Tools.ls.set('emMobilePassword', Base64.encode(toJS(this.password)))
    } else {
      Tools.ls.set('emMobilePassword', '')
    }

    let url = '/api/integration/simplesso/getToken'
    let params = {
      loginInfo: JSON.stringify({
        // type: accountLogin ? '1' : '2',
        type: '1',
        username: toJS(this.account),
        password: toJS(this.password),
        idcard: '',
        devid: ''
      })
    }
    if (
      !window.e9ssoMobileConfig ||
      !window.e9ssoMobileConfig.inUse ||
      !window.e9ssoMobileConfig.loginType ||
      window.e9ssoMobileConfig.loginType === 'commonSession'
    ) {
      url = '/api/hrm/login/checkLogin'
      params = {
        loginid: toJS(this.account),
        userpassword: toJS(this.password),
        logintype: 1,
        isie: false
      }
    }

    Tools.callApi({
      url: url,
      method: 'POST',
      params: params,
    }).then((res) => {
      if (
        !window.e9ssoMobileConfig ||
        !window.e9ssoMobileConfig.inUse ||
        !window.e9ssoMobileConfig.loginType ||
        window.e9ssoMobileConfig.loginType === 'commonSession'
      ) {
        if (res.loginstatus == 'true') {
          this.loginSuccess();
        } else {
          this.loginFail();
        }
      } else {
        if (res.status == true) {
          if (!res.ssoToken) {
            this.loginFail();
            return;
          }
          Tools.ls.set('customSessionKey', {
            keyName: 'ssoToken',
            keyValue: res.ssoToken
          })
          // window.location.href = unescape(urlParams['service']);
          if (window.e9ssoMobileConfig && window.e9ssoMobileConfig.inUse &&
            window.e9ssoMobileConfig.loginType && window.e9ssoMobileConfig.loginType === 'session') {
            Tools.callApi({
              url: '/api/integration/simplesso/getUser',
              method: 'POST',
              params: {
                ssoToken: res.ssoToken
              },
            }).then((ssoRes) => {
              if (ssoRes.status == true) {
                this.loginSuccess();
              } else {
                this.loginFail()
              }
            })
          } else {
            this.loginSuccess()
          }
        } else {
          this.loginFail()
        }
      }

    }).catch((res) => {
      Toast.hide();
      Toast.info(JSON.stringify(res));
    })
  }

}